package com.ouken.asynTask;

import java.io.InputStream;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import com.gen.tool.AlertDialog_PG;
import com.ouken.com.ForgetPwdActivity;
import com.ouken.com.HPActivity;
import com.ouken.com.LoginActivity;
import com.ouken.com.R;
import com.ouken.tool.ConnectServer;
import com.ouken.tool.MyApp;
import com.ouken.tool.Result;
import com.ouken.tool.StringToByte;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.TextView;



public class AysnGetAccount extends AsyncTask<String, TextView, Result> {
	private MyApp appState;
	private List params;
	private Context context;
	private String httpUrl;
	//private ProgressDialog dialog;
	
	private AlertDialog_PG PGDialog;
	private AlertDialog PGnow;

	/*public AysnGetAccount(MyApp appState,  Context context,AlertDialog PGnow) {
		super();
		this.appState = appState;
		this.params = new ArrayList();
		this.context = context;
		this.httpUrl = "http://"+ConnectServer.getServerAdd()+"/account";
		this.dialog = dialog;
	}*/
	
	public AysnGetAccount(MyApp appState,  Context context) {
		super();
		this.appState = appState;
		this.params = new ArrayList();
		this.context = context;
		this.httpUrl = "http://"+ConnectServer.getServerAdd()+"/account";
		
		PGDialog = new AlertDialog_PG(context,"Login success !! " +
				"\nPlease wait while " +
				"\nuser information loading...");
	}
	

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		System.out.println(" AysnGetAccount onPreExecute");
		
		final AlertDialog PGnow1 = PGDialog.AlertDialog_PG_show();
		PGnow = PGnow1;
		/*dialog.setMessage("Login success !! \nPlease wait while user information loading...");
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        dialog.show();*/
		
		params.add(new BasicNameValuePair("email", appState.getUserEmail()));
		params.add(new BasicNameValuePair("mac", appState.getMac()));
		params.add(new BasicNameValuePair("ParamEnd", "ParamEndParamEndParamEnd"));
	}
	
	@Override
	protected Result doInBackground(String... strings) {
		// TODO Auto-generated method stub
		
		System.out.println(" AysnGetAccount doInBackground");
		Result r = new Result();
		String s = ConnectServer.connect(httpUrl, params);
		r.setS(s);
		s =s.replaceAll("(\\ )", "");
		String[] resultParams =s.split(",");
		if(resultParams[0].toString().equals("OK")){
			Bitmap bitmap = ConnectServer.getBitmap(resultParams[3].toString());
			r.setBitmap(bitmap);
		}
		return r;
	}

	@Override
	protected void onPostExecute(Result result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		System.out.println(" AysnGetAccount onPostExecute");
		
		final AlertDialog PGnow_c = PGnow;
		
		if(result.getS() == null){
			PGnow_c.dismiss();
			//dialog.dismiss();
			 AlertDialog alertDialog =
					new AlertDialog.Builder(context)
	                .setIcon(R.drawable.alert_dialog_icon)
	                .setTitle("ERROR!")
	                .setMessage(R.string.ERROR_NETWORKOFF)
	                .setPositiveButton("OK",null)
	                .create();
				
					alertDialog.show();
			
			return;
		}
		String tmp =result.getS().replaceAll("(\\ )", "");
		String[] resultParams =tmp.split(",");
		if(resultParams[0].toString().equals("OK")){
			
			//byte[] aa = StringToByte.getByte(resultParams2[3].toString());
			//appState.setImg(BitmapFactory.decodeByteArray(aa, 0, aa.length)); 
			
			appState.setImg(result.getBitmap());
			appState.setNickName(resultParams[2]);
			appState.setTotalScore(resultParams[4]);
			Intent intent = new Intent(context, HPActivity.class);
			context.startActivity(intent);
			PGnow_c.dismiss();
			//dialog.dismiss();
			((Activity) context).finish();
		}else{
			PGnow_c.dismiss();
			//dialog.dismiss();
			AlertDialog alertDialog =
				new AlertDialog.Builder(context)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle("ERROR!")
                .setMessage(resultParams[1])
                .setPositiveButton("OK",null)
                .create();
			
				alertDialog.show();
		}
	}

	

}
